///////////////////////////////////////////////////////////
//  Pages.cpp
//  Implementation of the Class Pages
//  Created on:      21-Aug-2013 11:15:31 PM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "Pages.h"
#include "DataSchemaXML.h"

using namespace Data;


Pages::Pages()
{
  this->m_type = IObject::PAGES_T;
  this->m_name = _T("");
}


Pages::Pages(const Pages& anotherItem)
{
  this->m_type = IObject::PAGES_T;
  this->m_name = anotherItem.m_name;

  int countChilds = anotherItem.CountPages();
  for(int index = 0; index < countChilds; index++)
  {
    this->m_pageChilds.push_back(static_cast<IPage*>(anotherItem.GetPage(index)->Clone()));
  }
}


Pages::~Pages()
{
  std::vector<IPage*>::iterator childPage = this->m_pageChilds.begin();
  while(childPage != this->m_pageChilds.end())
  {
    delete (*childPage);
    (*childPage) = NULL;
    childPage++;
  }

  this->m_pageChilds.clear();
}


IObject::ObjectType Pages::GetType() const 
{
	return this->m_type;
}

String_t Pages::GetName() const
{
  return this->m_name;
}


void Pages::SetName( const String_t& name )
{
  this->m_name = name;
}


IPage* Pages::GetPage(unsigned int index) const 
{
	return this->m_pageChilds[index];
}


void Pages::AppendPage(IPage* page)
{
  page->SetParent(this);
  this->m_pageChilds.push_back(page);
}


int Pages::CountPages() const 
{
	return (int)this->m_pageChilds.size();
}


void Pages::RemovePage(unsigned int index)
{
  delete this->m_pageChilds[index];
  this->m_pageChilds[index] = NULL;
  this->m_pageChilds.erase(this->m_pageChilds.begin() + index);
}


IObject* Pages::Clone()
{
  IPages* clonePages = new Pages(*this);
	return clonePages;
}


String_t Pages::ToXML()
{
  String_t xml = _T("<") + DataSchemaXML::PAGES_ELEMENT;
  String_t context = this->m_name;
  replaceTextXML(context);

  if(!this->m_name.empty())
    xml += _T(" ") + DataSchemaXML::NAME_ATTR + _T("=\"") + context + _T("\">");
  else
    xml += _T(">");

  std::vector<IPage*>::iterator childPage = this->m_pageChilds.begin();
  while(childPage != this->m_pageChilds.end())
  {
    xml += (*childPage)->ToXML();
    childPage++;
  }

  xml += _T("</") + DataSchemaXML::PAGES_ELEMENT + _T(">");

	return  xml;
}

IPage* Pages::operator[]( unsigned int index )
{
  return this->m_pageChilds[index];
}


